package com.zuoyanit.unirtc;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.tencent.rtmp.ui.TXCloudVideoView;
import com.tencent.trtc.TRTCCloud;
import com.tencent.trtc.TRTCCloudDef;

import java.util.HashMap;
import java.util.Map;

import io.dcloud.feature.uniapp.UniSDKInstance;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.ui.action.AbsComponentData;
import io.dcloud.feature.uniapp.ui.component.AbsVContainer;
import io.dcloud.feature.uniapp.ui.component.UniComponent;
import io.dcloud.feature.uniapp.ui.component.UniComponentProp;

import static com.tencent.trtc.TRTCCloudDef.TRTC_VIDEO_STREAM_TYPE_BIG;

public class TxVideo  extends UniComponent<TXCloudVideoView> {

    private  String uid = "";

    public TxVideo(UniSDKInstance instance, AbsVContainer parent, AbsComponentData basicComponentData) {
        super(instance, parent, basicComponentData);

    }


    @Override
    protected TXCloudVideoView initComponentHostView(@NonNull Context context) {
        TXCloudVideoView txvideo = new TXCloudVideoView(context);
        return txvideo;
    }

    /**
     *
     * @param userid
     */
    @UniComponentProp(name = "userid")
    public void setUserId(String userid) {
        Log.e("txvideo", "ERROR--startRemotePreview1"+userid);
        this.uid = userid;
        startRemotePreview();
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> number = new HashMap<>();
        number.put("userid", userid);
        //目前uni限制 参数需要放入到"detail"中 否则会被清理
        params.put("detail", number);
        fireEvent("onUserid", params);
    }

    /**
     * 开启本地视频
     * true：前置摄像头,false：后置摄像头
     */
    @UniJSMethod
    public void  startLocalPreview(boolean frontCamera) {
        TRTCCloud.sharedInstance(getContext()).startLocalPreview(frontCamera, getHostView());
    }

    /***
     * 开启本地音频
     */
    @UniJSMethod
    public void startLocalAudio() {
        TRTCCloud.sharedInstance(getContext()).startLocalAudio(TRTCCloudDef.TRTC_AUDIO_QUALITY_DEFAULT);
    }

    /**
     * 开启远程视频
     */
    public void  startRemotePreview() {
        if(this.uid != "") {
            TRTCCloud mTRTCCloud = TRTCCloud.sharedInstance(getContext());
            mTRTCCloud.startRemoteView(this.uid, TRTC_VIDEO_STREAM_TYPE_BIG, getHostView());
            mTRTCCloud.setLocalViewFillMode(TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL);
        }
    }
    /**
     * 本地静音
     * @param mute  true：静音；false：取消静音
     */
    @UniJSMethod
    public void muteLocalAudio(boolean mute) {
        TRTCCloud.sharedInstance(getContext()).muteLocalAudio(mute);
    }


    /**
     * 远端静音
     * @param mute  true：静音；false：取消静音
     */
    @UniJSMethod
    public void muteRemoteAudio(boolean mute) {
        if(this.uid != "") {
            TRTCCloud.sharedInstance(getContext()).muteRemoteAudio(uid, mute);
        }
    }

    /**
     * 静音/取消静音所有用户的声音
     */
    public void muteAllRemoteAudio(boolean mute) {
        if(this.uid != "") {
            TRTCCloud.sharedInstance(getContext()).muteAllRemoteAudio(mute);
        }
    }







}
